package com.story.api.moduler.song.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.story.api.common.persistence.dao.SongCountMapper;
import com.story.api.common.persistence.model.SongCount;
import com.story.api.moduler.song.dao.SongCountDao;
import com.story.api.moduler.song.dao.VedioAgeDao;
import com.story.api.moduler.song.dao.VedioDetailDao;
import com.story.api.moduler.song.transfer.vo.SongCountVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Administrator
 * @decription 每天0点对各个年龄段的歌曲及歌曲总数进行统计
 * @Date 2017-12-14 14:58
 */
@Component
public class SongCountTask {

    private static final Logger log = LoggerFactory.getLogger(SongCountTask.class);

    @Autowired
    private VedioAgeDao vedioAgeDao;

    @Autowired
    private SongCountDao songCountDao;

    @Autowired
    private SongCountMapper songCountMapper;

    @Scheduled(cron = "0 0 0 * * ? ")
    public void updateSongCount() {
        SongCountVo songCountVo = vedioAgeDao.getSongCountByAgeType();
        int[] ageTypeArray = {1,2,3,0};
        for (int i = 0; i < ageTypeArray.length; i++) {
            SongCount songCount = new SongCount();
            songCount.setAgeType(ageTypeArray[i]);
            if(ageTypeArray[i] == 1) {
                songCount.setCount(songCountVo.getAgeOneCount());
            } else if(ageTypeArray[i] == 2) {
                songCount.setCount(songCountVo.getAgeOneToThreeCount());
            } else if(ageTypeArray[i] == 3) {
                songCount.setCount(songCountVo.getAgeThreeToSixCount());
            } else {
                songCount.setCount(songCountVo.getAllCount());
            }
            songCount.setUpdateTime(new Date());
            int count = songCountDao.existCountByAge(ageTypeArray[i]);
            if(count == 0) {
                log.info("ageType="+ageTypeArray[i]+"的歌曲数更新");
                songCount.setCreateTime(new Date());
                songCountMapper.insert(songCount);
            }else {
                log.info("ageType="+ageTypeArray[i]+"的歌曲数更新");
                songCountMapper.update(songCount,new EntityWrapper<SongCount>().eq("ageType",ageTypeArray[i]));
            }
        }
    }

}
